$(document).ready(function(){
	getUserInfo();
	$('#logoutLink').click(function(){logOut();return false;});	
	$('#amodifyPwd').click(function(){pwdChgHTML(false);return false;});
});


/*获取登陆用户信息*/
function getUserInfo(){
	var refObj = new Object();	 
	ajaxGet(contextPath+"/Account/UserInfo",refObj,'getUserInfoCallBack(refObj);');
}

function getUserInfoCallBack(data){
	if(data){	 
		$('#userName').html(data.userName);		 
	}
}

/* 登出       */ 
function logOut(){
	var refObj = new Object();
	ajaxGet(contextPath + "/Account/LogOff", refObj, 'userLogOutCallBack(refObj);');
}

function userLogOutCallBack(data){
	if(data){
	    location = contextPath + '/Account/LogOn';
	}
}

function changePswValidate(oPsw,nPsw,enPsw){		 
	if(oPsw == ""){
		$(".error").show().html("原始密码不可为空，请重试！");	
		return false;
	}
	if(nPsw == ""){
		$(".error").show().html("新密码不可为空，请重试！");	
		return false;
	}
	if(enPsw == ""){
		$(".error").show().html("确认新密码不可为空，请重试！");
		return false;	
	}
	if(GetLen(nPsw) != 6){
		$(".error").show().html("密码长度必须是6位，请重试！");
		return false;		
	}	 
	if(enPsw != nPsw){
		$(".error").show().html("两次输入密码不一致，请重试！");
		return false;	
	}	
	else if(oPsw && nPsw && enPsw){
		return true;
	}	 
	return false;
}

function changePswClose(){
	var oPsw = $("#oPsw");
	var nPsw = $("#nPsw");
	var enPsw = $("#enPsw"); 
	if(changePswValidate($.trim(oPsw.val()),$.trim(nPsw.val()),$.trim(enPsw.val()))){
		var refObj = new Object();
		ajaxPost(contextPath + "/Account/ChangePassword", { oldPassword: oPsw.val(), password: enPsw.val() }, refObj, 'changePasswordCallBack(refObj)');		
	}
}

function changePasswordCallBack(data){
	if(data && data.result=='success'){		
		changePswCancel();
		alert("恭喜，修改密码成功！");
	}
	if (data && data.result == 'failture') {
		if(data.msg=='orginal password inconsistent'){
			$(".error").show().html('旧密码输入不正确!');
		} else{
			$(".error").show().html(data.msg);
		}
	}
}

function changePswCancel(){
	$(".error").hide();
	$("#bg").remove();
	$(".changeInfo").remove();	
}

/*点击头部修改密码*/
function pwdChgHTML(isfirst){	
	var jqCombine = new StringBuilder();
	jqCombine.append("<div id='bg' class='bg'><\/div><div class='changeInfo'><div class='changeHd'>修改密码<\/div><div class='changeCn'>");	 
	if(isfirst){ 
		jqCombine.append("<h4>为了您的账户安全，请修改登录密码<\/h4>");
	}	
	jqCombine.append("<table class='changeTabInfo' cellpadding='0' cellspacing='0'><tbody><tr><td class='txtRt'>原始密码：<\/td><td><input class='input' id='oPsw' type='password' value='' \/><\/td><\/tr><tr><td class='txtRt'>新密码：<\/td><td><input class='input' id='nPsw' type='password' value='' \/><\/td><\/tr><tr><td class='txtRt'>确认新密码：<\/td><td><input class='input' id='enPsw' type='password' value='' \/><\/td><\/tr><tr><td><\/td><td class='errorTxt clearfix'><p class='error'>错误信息提示<\/p><\/td><\/tr><\/tbody><\/table><div class='changeBtn clearfix'>");
	if(!isfirst){
		jqCombine.append("<a href='javascript:;' onclick='changePswCancel();'>取消<\/a>");
	}
	jqCombine.append("<a class='changeEnter' href='javascript:;'onclick='changePswClose();'><\/a><\/div><\/div><\/div>");
	$("body").append(jqCombine.toString());	
	var de = document.documentElement;
	var clientH=de.scrollHeight;
	var clientW=de.clientWidth;
	$("#bg").width(clientW+"px").height(clientH+"px").show();
	if(isfirst){
		$(".changeInfo").css({"top":(clientH/2-160)+"px","left":(clientW/2-210)+"px"}).show();	
		$(".changeBtn").width(54);
	}else{
		$(".changeInfo").css({"top":(clientH/2-160)+"px","left":(clientW/2-210)+"px","height":290+'px'}).show();
		$(".changeBtn").width(100);
	}	
}

